﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace HousingDistribution
{
    partial class FormPersonDetailsNoHousingTab
    {
        private void InitDGVPersonSearch()
        {
            //规定不自动生成列
            this.dataGridViewPersonSearch.AutoGenerateColumns = false;

            this.dataGridViewPersonSearch.Columns.Add(PersonInfoDTConsts.ID[0], PersonInfoDTConsts.ID[1]);
            this.dataGridViewPersonSearch.Columns.Add(PersonInfoDTConsts.NAME[0], PersonInfoDTConsts.NAME[1]);
            this.dataGridViewPersonSearch.Columns.Add(PersonInfoDTConsts.TYPE[0], PersonInfoDTConsts.TYPE[1]);
            this.dataGridViewPersonSearch.Columns.Add(PersonInfoDTConsts.UNIT[0], PersonInfoDTConsts.UNIT[1]);

            this.dataGridViewPersonSearch.Columns[PersonInfoDTConsts.ID[0]].Visible = false;
            this.dataGridViewPersonSearch.Columns[PersonInfoDTConsts.NAME[0]].HeaderText = PersonInfoDTConsts.NAME[1];
            this.dataGridViewPersonSearch.Columns[PersonInfoDTConsts.TYPE[0]].HeaderText = PersonInfoDTConsts.TYPE[1];
            this.dataGridViewPersonSearch.Columns[PersonInfoDTConsts.UNIT[0]].HeaderText = PersonInfoDTConsts.UNIT[1];

            this.dataGridViewPersonSearch.Columns[PersonInfoDTConsts.NAME[0]].Width = 55;
            this.dataGridViewPersonSearch.Columns[PersonInfoDTConsts.TYPE[0]].Width = 60;
            this.dataGridViewPersonSearch.Columns[PersonInfoDTConsts.UNIT[0]].Width = 150;

            //bind data
            this.dataGridViewPersonSearch.Columns[PersonInfoDTConsts.ID[0]].DataPropertyName = PersonInfoDTConsts.ID[0];
            this.dataGridViewPersonSearch.Columns[PersonInfoDTConsts.NAME[0]].DataPropertyName = PersonInfoDTConsts.NAME[0];
            this.dataGridViewPersonSearch.Columns[PersonInfoDTConsts.TYPE[0]].DataPropertyName = PersonInfoDTConsts.TYPE[0];
            this.dataGridViewPersonSearch.Columns[PersonInfoDTConsts.UNIT[0]].DataPropertyName = PersonInfoDTConsts.UNIT[0];
        }

        private void textBoxSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Only Accept a char, or backspace
            if ((Char.IsLetter(e.KeyChar) || e.KeyChar == 8))
            {
                //If we are not overwriting the whole text
                if (this.textBoxSearch.SelectionLength != this.textBoxSearch.Text.Length)
                {
                    //Check that the new Text value will be a valid
                    // ip octet then move on to next box
                    if (e.KeyChar == 8)
                    {
                        Query(this.textBoxSearch.Text.Remove(this.textBoxSearch.Text.Length - 1, 1));
                    }
                    else if (this.textBoxSearch.Text.Length == 3)
                    {
                        this.textBoxSearch.SelectAll();
                        e.Handled = true;
                    }
                    else
                    {
                        Query(this.textBoxSearch.Text + e.KeyChar.ToString());
                    }
                }//首字母
                else if (this.textBoxSearch.Text.Length == 0)
                {
                    Query(e.KeyChar.ToString());
                }
            }
            //Do nothing if the keypress is not a char, or SPLIT_STR
            else
                e.Handled = true;
        }

        private void Query(string name)
        {
            if (name.Length > 0)
            {
                StringBuilder sql = new StringBuilder(PERSON_QUERY_SQL_HEADER);
                sql.Append(GetNameInitialQueryString(name));
                sql.Append("order by idperson desc limit 0, ");
                sql.Append(MAX_COUNT.ToString());

                System.Console.WriteLine(sql.ToString());
                //重新设置读取数据库中总人数（该条件下）
                this.personCount = GetPersonCount();

                this.personTable = DBProc.Instance().ReadData(sql.ToString()).Tables[0];

                this.dataGridViewPersonSearch.DataSource = this.personTable;
            }
            else
                this.dataGridViewPersonSearch.DataSource = null;
        }


        private void dataGridViewPersonSearch_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            int colIndex = e.ColumnIndex;

            if (rowIndex < 0 || colIndex < 0)
                return;

            this.curIndex = rowIndex;

            DisplayInfo(this.curIndex);
        }

        private string GetNameInitialQueryString(string name)
        {
            if (name.Length == 0)
                return "and 1=1 ";

            StringBuilder sql = new StringBuilder("and person.nameInitial like '");
            sql.Append(name.ToUpper());
            sql.Append("%' ");

            return sql.ToString();
        }
    }
}
